/*
 * Created on May 31, 2005
 *
 */
package com.erclab.internal.xpresso.unittests;

import com.erclab.internal.xpresso.*;
import com.erclab.internal.xpresso.forms.*;
import com.erclab.internal.xpresso.formelements.*;
import com.erclab.internal.xpresso.reports.*;
import com.erclab.internal.xpresso.reportelements.*;
import com.erclab.internal.xpresso.security.User;
import com.erclab.internal.xpresso.conf.Settings;
import com.erclab.internal.xpresso.dataAccess.DataModifier;
import com.erclab.internal.xpresso.datavalidators.DataValidationError;
import com.erclab.internal.xpresso.exceptions.ApplicationException;
import com.erclab.internal.xpresso.exceptions.DataModificationException;
import com.erclab.internal.xpresso.exceptions.DirectoryException;
import com.erclab.internal.xpresso.exceptions.FormException;

import java.util.*;

import junit.framework.TestCase;

/**
 * @author Enrique Rodriguez.
 *
 */
public class MySqlApplicationTest extends XpressoBaseTest {
	

	public void testCreateApplication(){
		System.out.println("testCreateApplication() -------------------------");
		assertTrue(grad.getApplicationName() != null);
		assertTrue(grad.getApplicationDescription() != null);
		assertTrue(Settings.getAdminMode());
		
		System.out.println("ApplicationName: " + grad.getApplicationName());
		System.out.println("ApplicationDescription: " + grad.getApplicationDescription());
		
	}
	
	public void testCreateForm(){
		System.out.println("testCreateAForm() -------------------------");
		// Create a form using a form factory
		Form theForm = null;
		try {
			theForm = FormFactory.loadForm("Reports");
		} catch (FormException e1) {
			e1.printStackTrace();
			fail();
		}
		assertTrue(theForm != null);
		assertTrue(theForm.getFormName().equals("Reports"));

		System.out.println("Form info :");
		System.out.println(theForm.toString());
		

		
		User user = null;
		try {
			Settings.getUserDirectory().loadUserBean(user);
		} catch (DirectoryException e) {
			e.printStackTrace();
			fail();
		}
		
		FormParameters parameters = new FormParameters();
		parameters.setParentKeyValue(null);
		parameters.setUniqueKeyValue("19");
		parameters.setAction("insert");
		parameters.setUser(user);

		theForm.setFormParameters(parameters);
		
		// Load form options:
		try {
			OptionsFactory.loadFormOptions(theForm, user);
		} catch (Exception e2) {
			e2.printStackTrace();
			fail();
		}
		
		FormResultSet frs = null;
		try {
			frs = FormFactory.createForm(theForm);
		} catch (Exception e) {
			e.printStackTrace();
			fail();
		}
		// Test and print the elements
		ArrayList formElements = theForm.getFormElements();
		if (formElements == null){
			System.out.println("No form elements for this form");
		} 
		
		for (Iterator iter = formElements.iterator(); iter.hasNext();) {
			FormElement fe = (FormElement)iter.next();
			System.out.println("Destination of the element -->" + fe.getDestinationColumn());
			System.out.println("The element to html -->" + fe.toHTML(theForm));
		}
		
		while (frs.nextFormElement()){
			System.out.println(frs.getLabel() + " --> " + frs.getElement());			
		}
	}
	
	public void testCreateReport(){
		System.out.println("testCreateReport() ---------------------");
		try {
			Settings.getUserDirectory().loadUserBean(user);
		} catch (DirectoryException e) {
			fail();
			e.printStackTrace();
		}


		ReportParameters parameters = new ReportParameters();
		parameters.setConditions("");
		parameters.setPageNum(1);
		parameters.setParentKeyValue(null);
		parameters.setUniqueKeyValue("0");
		parameters.setUser(user);

		
		Report theReport = null;
		try {
			theReport = ReportFactory.loadReport("Reports");
		} catch (Exception e1) {
			e1.printStackTrace();
			fail();
		}
		System.out.println(theReport);
		
		// Load the parameters of the report
		theReport.setReportParameters(parameters);
		
		// Test and print the elements
		ArrayList reportElements = theReport.getReportElements();
		if (reportElements == null){
			System.out.println("No form elements for this report");
		} else {
			for (Iterator iter = reportElements.iterator(); iter.hasNext();) {
				ReportElement re = (ReportElement)iter.next();
				System.out.println("Report Element Header: " + re.getHeaderText());
			}
		}
		
		// Load report options:
		try {
			OptionsFactory.loadReportOptions(theReport, user);
		} catch (Exception e2) {
			e2.printStackTrace();
			fail();
		}
		
		ReportResultSet rrs = null;
		try {
			rrs = ReportFactory.executeReportQuery(theReport);
		} catch (Exception e) {
			e.printStackTrace();
			fail();
		}
		
		// Print result set:
		assertTrue(rrs != null);
		for (int i =0; i < rrs.getNumberOfColumns(); i++){
			System.out.print(rrs.getHeaderText(i) + " | ");
		}
		while(rrs.nextRow()){
			System.out.println("");
			for (int i =0; i < rrs.getNumberOfColumns(); i++){
				System.out.print(rrs.getRowValue(i) + " | ");
			}			
		}
	}
	
	public void testUserSetup(){
		System.out.println("testUserSetup() ---------------------");
		try {
			Settings.getUserDirectory().loadUserBean(user);
		} catch (DirectoryException e) {
			fail();
			e.printStackTrace();
		}
		assertTrue(user.getUserName() != null);
		System.out.println("netId: " + user.getNetId() + " - Name: " + user.getUserName());
		if (user.getRoles() != null){
			for (int i=0; i< user.getRoles().length; i++){
				System.out.println("role: " + user.getRoles()[i]);
			}
		}
	}

	public void testFormUpdate(){
		System.out.println("testFormUpdate() ---------------------");
		User user = null;
		try {
			Settings.getUserDirectory().loadUserBean(user);
		} catch (DirectoryException e) {
			fail();
			e.printStackTrace();
		}
		
		
		FormParameters parameters = new FormParameters();
		parameters.setParentKeyValue(null);
		parameters.setUniqueKeyValue("213");
		parameters.setAction("update");
		parameters.setUser(user);

		Form theForm = null;
		try {
			theForm = FormFactory.loadForm("Reports");
		} catch (FormException e) {
			fail();
		}
		theForm.setFormParameters(parameters);
		
		
		HashMap keyValues = new HashMap();
		for (Iterator iter = theForm.getFormElements().iterator(); iter.hasNext();) {
			FormElement fe = (FormElement)iter.next();
			keyValues.put("Field" + fe.getIdFormElement(), new String[]{"0"});
		}
		
		DataModifier dm = new DataModifier();
		try {
			dm.executeFormChange(theForm, keyValues);
		} catch (DataModificationException e) {
			e.printStackTrace();
			fail();
		} catch (DataValidationError e) {
			e.printStackTrace();
			fail();
		}
	}

	public void testFormInsert(){
		System.out.println("testFormUpdate() ---------------------");
		User user = null;
		try {
			Settings.getUserDirectory().loadUserBean(user);
		} catch (DirectoryException e) {
			fail();
			e.printStackTrace();
		}
		
		
		FormParameters parameters = new FormParameters();
		parameters.setParentKeyValue(null);
		parameters.setUniqueKeyValue("19");
		parameters.setAction("insert");
		parameters.setUser(user);

		Form theForm = null;
		try {
			theForm = FormFactory.loadForm("Reports");
		} catch (FormException e) {
			fail();
		}
		theForm.setFormParameters(parameters);
				
		HashMap keyValues = new HashMap();
		for (Iterator iter = theForm.getFormElements().iterator(); iter.hasNext();) {
			FormElement fe = (FormElement)iter.next();
			keyValues.put("Field" + fe.getIdFormElement(), new String[]{"0"});
		}
		
		DataModifier dm = new DataModifier();
		try {
			dm.executeFormChange(theForm, keyValues);
		} catch (DataModificationException e) {
			e.printStackTrace();
			fail();
		} catch (DataValidationError e) {
			e.printStackTrace();
			fail();
		}
	}

	public void testReportDelete(){
		Report theReport = null;
		try {
			theReport = ReportFactory.loadReport("Reports");
		} catch (Exception e1) {
			e1.printStackTrace();
			fail();
		}
		
		
		DataModifier dm = new DataModifier();
		try {
			dm.deleteElements(theReport, user, new String[]{"0"});
		} catch (DataModificationException e) {
			e.printStackTrace();
			fail();
		}
		
	}
	
}


